var NumberFormatUtil = (function() {
	//保留两位小数     
	//功能：将浮点数四舍五入，取小数点后2位    
	function toDecimal(x) {    
	    var f = parseFloat(x);    
	    if (isNaN(f)) {    
	        return;    
	    }    
	    f = Math.round(x*100)/100;    
	    return f;    
	}    
	  
	  
	//制保留2位小数，如：2，会在2后面补上00.即2.00    
	function toDecimal2(x) {    
	    var f = parseFloat(x);    
	    if (isNaN(f)) {    
	        return false;    
	    }    
	    var f = Math.round(x*100)/100;    
	    var s = f.toString();    
	    var rs = s.indexOf('.');    
		if (rs < 0) {    
		    rs = s.length;    
		    s += '.';    
		}    
		while (s.length <= rs + 2) {    
		    s += '0';    
	    }    
	    return s;    
	}    

	function fomatFloat(src,pos){       
	     return Math.round(src*Math.pow(10, pos))/Math.pow(10, pos);       
	}

    /*
    * 参数说明：
    * number：要格式化的数字
    * decimals：保留几位小数
    * dec_point：小数点符号
    * thousands_sep：千分位符号
    * roundtag:舍入参数，默认 "ceil" 向上取,"floor"向下取,"round" 四舍五入
    */
    function formatNumber(number, decimals, dec_point, thousands_sep,roundtag) {
        // console.log(number_format(2, 2, ".", ","))//"2.00"
        // console.log(number_format(3.7, 2, ".", ","))//"3.70"
        // console.log(number_format(3, 0, ".", ",")) //"3"
        // console.log(number_format(9.0312, 2, ".", ","))//"9.03"
        // console.log(number_format(9.00, 2, ".", ","))//"9.00"
        // console.log(number_format(39.715001, 2, ".", ",", "floor")) //"39.71"
        // console.log(number_format(9.7, 2, ".", ","))//"9.70"
        // console.log(number_format(39.7, 2, ".", ","))//"39.70"
        // console.log(number_format(9.70001, 2, ".", ","))//"9.71"
        // console.log(number_format(39.70001, 2, ".", ","))//"39.71"
        // console.log(number_format(9996.03, 2, ".", ","))//"9996.03"
        // console.log(number_format(1.797, 3, ".", ",", "floor"))//"1.797"
        number = (number + '').replace(/[^0-9+-Ee.]/g, '');
        roundtag = roundtag || "ceil"; //"ceil","floor","round"
        var n = !isFinite(+number) ? 0 : +number,
            prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
            dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s = '',
            toFixedFix = function (n, prec) {

                var k = Math.pow(10, prec);
                console.log();

                return '' + parseFloat(Math[roundtag](parseFloat((n * k).toFixed(prec*2))).toFixed(prec*2)) / k;
            };
        s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
        var re = /(-?\d+)(\d{3})/;
        while (re.test(s[0])) {
            s[0] = s[0].replace(re, "$1" + sep + "$2");
        }

        if ((s[1] || '').length < prec) {
            s[1] = s[1] || '';
            s[1] += new Array(prec - s[1].length + 1).join('0');
        }
        return s.join(dec);
    }
    /*
     * formatMoney(num,type)
     * 功能：金额按千位逗号分割,四舍五入保留两位小数
     * 参数：num，需要格式化的金额数值.
     * 返回：返回格式化后的数值字符串.
     */
    function formatMoney(num) {
        return formatNumber(num, 2, ".", ",", "round");
    }

    // 返回数字
    function removeFormatMoney(s) {
        return parseFloat(s.replace(/[^\d\.-]/g, ""));
    }

    // 把小写的金额转换成大写的金额 add by zhengxb 2019.2.27  
    // 源代码copy地址： https://blog.csdn.net/zz_chst/article/details/79325440
    
    function convertCurrencyToBig(money) {  
	  //汉字的数字  
	  var cnNums = new Array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖');  
	  //基本单位  
	  var cnIntRadice = new Array('', '拾', '佰', '仟');  
	  //对应整数部分扩展单位  
	  var cnIntUnits = new Array('', '万', '亿', '兆');  
	  //对应小数部分单位  
	  var cnDecUnits = new Array('角', '分', '毫', '厘');  
	  //整数金额时后面跟的字符  
	  var cnInteger = '整';  
	  //整型完以后的单位  
	  var cnIntLast = '元';  
	  //最大处理的数字  
	  var maxNum = 999999999999999.9999;  
	  //金额整数部分  
	  var integerNum;  
	  //金额小数部分  
	  var decimalNum;  
	  //输出的中文金额字符串  
	  var chineseStr = '';  
	  //分离金额后用的数组，预定义  
	  var parts;  
	  if (money == '') { return ''; }  
	  money = parseFloat(money);  
	  if (money >= maxNum) {  
	    //超出最大处理数字  
	    return '';  
	  }  
	  if (money == 0) {  
	    chineseStr = cnNums[0] + cnIntLast + cnInteger;  
	    return chineseStr;  
	  }  
	  //转换为字符串  
	  money = money.toString();  
	  if (money.indexOf('.') == -1) {  
	    integerNum = money;  
	    decimalNum = '';  
	  } else {  
	    parts = money.split('.');  
	    integerNum = parts[0];  
	    decimalNum = parts[1].substr(0, 4);  
	  }  
	  //获取整型部分转换  
	  if (parseInt(integerNum, 10) > 0) {  
	    var zeroCount = 0;  
	    var IntLen = integerNum.length;  
	    for (var i = 0; i < IntLen; i++) {  
	      var n = integerNum.substr(i, 1);  
	      var p = IntLen - i - 1;  
	      var q = p / 4;  
	      var m = p % 4;  
	      if (n == '0') {  
	        zeroCount++;  
	      } else {  
	        if (zeroCount > 0) {  
	          chineseStr += cnNums[0];  
	        }  
	        //归零  
	        zeroCount = 0;  
	        chineseStr += cnNums[parseInt(n)] + cnIntRadice[m];  
	      }  
	      if (m == 0 && zeroCount < 4) {  
	        chineseStr += cnIntUnits[q];  
	      }  
	    }  
	    chineseStr += cnIntLast;  
	  }  
	  //小数部分  
	  if (decimalNum != '') {  
	    var decLen = decimalNum.length;  
	    for (var i = 0; i < decLen; i++) {  
	      var n = decimalNum.substr(i, 1);  
	      if (n != '0') {  
	        chineseStr += cnNums[Number(n)] + cnDecUnits[i];  
	      }  
	    }  
	  }  
	  if (chineseStr == '') {  
	    chineseStr += cnNums[0] + cnIntLast + cnInteger;  
	  } else if (decimalNum == '') {  
	    chineseStr += cnInteger;  
	  }  
	  return chineseStr;  
	} 
    
	return {
		toDecimal: toDecimal,
		toDecimal2: toDecimal2,
		fomatFloat: fomatFloat,
        formatNumber: formatNumber,
        formatMoney: formatMoney,
        removeFormatMoney: removeFormatMoney,
        convertCurrencyToBig:convertCurrencyToBig
	};
})();

